/*
 * Copyright (c) 2013-2013 XXXX XXXXXX, Inc.
 * XXXX Road, XXXX, XXXX State, XXXX Post code, XXXX Country.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of XXXX
 * XXXXXX, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with XXXX.
 * 
 * History
 * Date        Ver Author        Change Description
 * ----------- --- ------------- ----------------------------------------
 * 03 May 2013 001 Karl          Create
 * 07 Aug 2013 002 Karl          Refactor code, add homePage field.
 */
package com.selenium.test.base.edu;

import com.selenium.test.base.SeleniumTestAbstract;
import com.selenium.test.cases.pages.rcl.HomePage;
import com.selenium.test.cases.pages.rcl.LoginPage;
import com.selenium.test.cases.pages.rcl.LoginPage.UserNotLoggedInException;
import com.selenium.test.cases.pages.rcl.pageUtil.HomePageUtil;
import com.selenium.test.cases.pages.rcl.pageUtil.LoginPageUtil;
import com.selenium.test.util.SeleniumConfig;
import com.selenium.test.util.SeleniumTestDataHolder;

/** 
 * for home page test convenience only. 
 * 
 * a single login() call will make browser login home page
 */
public abstract class HomePageTestAbstract extends SeleniumTestAbstract {
	
	/** currently used login user name*/
	protected String userName;
	
	/** currently used password*/
	protected String password;
	
	protected HomePage homePage;
	protected HomePageUtil homePageUtil;
	
	/** 
	 * for quick test only <br/><br/>
	 * 
	 * If a specific sub object need to use different user name and password, 
	 * use another constructor instead. 
	 */
	public HomePageTestAbstract(){
		try{
			userName = SeleniumTestDataHolder.getSystemLevelProperty(SeleniumConfig.TEST_USERNAME_KEY);
			password = SeleniumTestDataHolder.getSystemLevelProperty(SeleniumConfig.TEST_PASSWORD_KEY);
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	/** assign user name and password version*/
	public HomePageTestAbstract(String userName, String password){
		this.userName = userName;
		this.password = password;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	/** open home page, and login*/
	protected HomePage login(){
		LoginPage loginPage = LoginPage.enter(seleniumUtil);
		try {
			homePage = new LoginPageUtil(seleniumUtil, loginPage)
				.login(userName, password);
			homePageUtil = new HomePageUtil(seleniumUtil, homePage);
			return homePage;
		} catch (UserNotLoggedInException e) {
			throw new AssertionError(e.getMessage());
		}
	}
	
	/** open home page, and login*/
	protected HomePage login(String userName, String password){
		this.userName = userName;
		this.password = password;
		return login();
	}
}
